import os
import pandas as pd
from tqdm import tqdm

rootdir ='../../xuelangyun_d/Motor_tain/'
dir = os.listdir(rootdir)
f = open('train_xiao.csv', 'w')
for i in range(28):
	f.write(str(i+1)+",")
f.write("y"+"\n")

for dirname in dir :
	if dirname=="Positive":
		label0 = "1"
	else:
		label0 = "0"
	# print(os.path.join(rootdir,dirname))
	list = os.listdir(os.path.join(rootdir,dirname))
	names = []
	for i in range(len(list)):
		name = list[i].split("_")[0]
		names.append(name)
	names =set(names)

	def stat_feature_build(sr):
		l = len(sr)
		Min = sr.min()
		Max = sr.max()
		Median = sr.median()
		Mean = sr.mean()
		Std = sr.std()
		Skew = sr.skew()
		Kurt = sr.kurt()
		return Min, Max, Mean, Median, Std, Skew, Kurt

	def write(Min, Max, Mean, Median, Std, Skew, Kurt,flag,label):
		s = str(Min[0]) + "," + str(Min[1]) +","+ str(Max[0]) + "," + str(Max[1]) +","+str(Mean[0]) + "," \
		    + str(Mean[1]) +","+ str(Median[0]) + "," + str(Median[1])+"," +str(Std[0]) + "," + str(Std[1]) +"," +\
		    str(Skew[0]) + "," + str(Skew[1]) +","+	str(Kurt[0]) + "," + str(Kurt[1])+","
		if(flag==","):
			f.write(s)
		else:
			f.write(s + label + flag)

	for name in tqdm(names):
		s1 = os.path.join(rootdir+"\\"+dirname, str(name)+"_B.csv")
		s2 = os.path.join(rootdir+"\\"+dirname, str(name) + "_F.csv")
		data1 = pd.read_csv(open(s1))
		Min, Max, Mean, Median, Std, Skew, Kurt = stat_feature_build(data1)
		write(Min, Max, Mean, Median, Std, Skew, Kurt,",",label0)

		data2 = pd.read_csv(open(s2))
		Min, Max, Mean, Median, Std, Skew, Kurt = stat_feature_build(data2)
		write(Min, Max, Mean, Median, Std, Skew, Kurt,"\n",label0)
f.close()

